// pages/Mine/Mine.js
const app = getApp();
var utils = require("../../utils/utils.js");
var server = require("../../utils/server.js");
var config = require("../../config.js");
var apis = require("../../utils/api.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userIcon: '/image/user_icon.png',
    company: '',
    mobile: '***********',
    nickname: '',
    authStatus: '',
    entName: '',
    tabList: [{
        "pagePath": "/pages/Main/Main",
        "text": "首页",
        "iconPath": "/image/icon_main_nocheck.png",
        "selectedIconPath": "/image/icon_main.png"
      },
      {
        "pagePath": "/pages/Ocr/Ocr",
        "text": "识别",
        "iconPath": "/image/icon_pz_nocheck.png",
        "selectedIconPath": "/image/icon_pz.png"
      },
      {
        "pagePath": "/pages/Mine/Mine",
        "text": "我的",
        "iconPath": "/image/icon_me_nocheck.png",
        "selectedIconPath": "/image/icon_me.png"
      }
    ],
    currentTab: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo()
    this.getUserEntInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  toAuthApply() {
    if (app.globalData.userInfo.authStatus != 1) {
      utils.showToast('您还未正式通过企业认证，请稍后再试')
    } else {
      wx.navigateTo({
        url: '/pages/AuthApply/AuthApply',
      })
    }
  },

  toMessage() {
    if (app.globalData.userInfo.authStatus != 1) {
      utils.showToast('您还未正式通过企业认证，请稍后再试')
    } else {
      wx.navigateTo({
        url: '/pages/Message/Message',
      })
    }
  },

  toCall() {
    const phoneNumber = '13357672125';
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function () {
        console.log('拨打电话成功');
      },
      fail: function () {
        console.log('拨打电话失败');
      }
    });
  },

  toAboutUs() {
    if (app.globalData.userInfo.authStatus != 1) {
      utils.showToast('您还未正式通过企业认证，请稍后再试')
    } else {
      wx.navigateTo({
        url: '/pages/AboutUs/AboutUs',
      })
    }
  },

  toUserInfo() {
    wx.navigateTo({
      url: '/pages/UserInfo/UserInfo',
    })
  },

  getUserInfo() {
    var _this = this;
    var prams = {
      id: app.globalData.userId
    }
    var api = apis.getUserInfo;
    server.getRequest(config.xburl, api, prams,
      function (res) {
        app.globalData.userInfo.authStatus = res.data.authStatus,
          app.globalData.userInfo.cardNo = res.data.cardNo,
          app.globalData.userInfo.entId = res.data.entId,
          app.globalData.userInfo.nickname = res.data.nickname
        app.globalData.userInfo.avatar = res.data.avatar
        app.globalData.userInfo.mobile = res.data.mobile
        app.globalData.userInfo.userType = res.data.userType
        console.log('查询用户信息', app.globalData.userInfo)
        let mobile = res.data.mobile
        mobile = mobile ? mobile.replace(/(\d{3})\d*(\d{4})/, '$1****$2') : '未注册'
        _this.setData({
          authStatus: res.data.authStatus,
          nickname: res.data.nickname,
          mobile: mobile
        })
      },
      function (err) {
        utils.showToast(err)
      })
  },

  getUserEntInfo() {
    var _this = this;
    var prams = {
      id: app.globalData.userId
    }
    var api = apis.getUserEntInfo;
    server.getRequest(config.xburl, api, prams,
      function (res) {
        app.globalData.userInfo.entName = res.data.entName
        _this.setData({
          entName: res.data.entName
        })
      },
      function (err) {
        utils.showToast(err)
      })
  },
})